home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Thread Manager / Thread Manager 2.1.1d1+ / ThreadedSort / Sprocket / Lib / Window.cp < prev   
Encoding:
Text File  |  1995-04-28  |  22.6 KB  |  914 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Window.cp
  3.  
  4.     Contains:    Implementation of TWindow, a base class which provides a
  5.                 framework for building way-cool windows which even John
  6.                 Sullivan would be happy with. Floating windows and “smart
  7.                 zooming” algorithms are based on code samples provided by
  8.                 Dean Yu. Tim Craycroft, the guy making the window manager
  9.                 do all this work for you has also been a great help.
  10.                 
  11.     Written by: Dave Falkenburg
  12.  
  13.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  14.  
  15.     Change History (most recent first):
  16.     
  17.          <8>    11/17/94    DRF        Add casts for CFront & PPCC. Also dealt with the change to
  18.                                     ClipAbove in the latest universal headers.
  19.          <7>    11/12/94    DRF        Added AdjustMenusBeforeMenuSelection.
  20.          <6>     11/8/94    DRF        Add some better menu handling methods.
  21.          <5>     9/27/94    DRF         AppLib.h is now Sprocket.h
  22.          <4>      9/9/94    DRF        Reorganized headers and removed redundant #includes.
  23.          <3>      9/4/94    DRF        Added DrawJustTheGrowIcon.
  24.          <2>     8/27/94    DRF        In TWindow::Close, call window’s (de)Activate method before
  25.                                     closing so that menus can be properly updated.
  26.     
  27.     To Do:        Make sure invisible windows can be created & managed
  28.                 Handle modal windows as another class of windows
  29.                 Fix activate bugs when showing and hiding windows
  30.                 Window positioning methods (getters and setters)
  31.                 Display Manager support
  32.                 Changes to support AEObject model
  33.  */
  34.  
  35. #include "Sprocket.h"
  36. #include "Window.h"
  37.  
  38. #include <Script.h>        //    for GetMBarHeight()
  39. #include <LowMem.h>        //    for LMGetWindowList()
  40.  
  41.  
  42. const short            kFloatingWindowKind        = 1000;
  43. const short            kNormalWindowKind        = 1001;
  44. const WindowPtr     kNoFloatingWindows        = (WindowPtr) -1;
  45.  
  46. const short            kScreenEdgeSlop            = 4;
  47. const short            kSpaceForFinderIcons    = 64;
  48. const short            kMinimumTitleBarHeight    = 21;
  49. const short            kMinimumWindowSize        = 32;
  50.  
  51. static void            HiliteShowHideFloatingWindows(Boolean hiliting,Boolean hiding);
  52.  
  53. static void            FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect, GDHandle * theBestDevice);
  54. static pascal void    CalculateWindowAreaOnDevice(short depth,short deviceFlags,GDHandle targetDevice,long userData);
  55.  
  56.  
  57. struct    CalcWindowAreaDeviceLoopUserData
  58.     {
  59.     GDHandle    fScreenWithLargestPartOfWindow;
  60.     long        fLargestArea;
  61.     Rect        fWindowBounds;
  62.     };
  63.  
  64.  
  65.  
  66.  
  67.  
  68. TWindow::TWindow()
  69.     {
  70.     }
  71.  
  72.  
  73. TWindow::~TWindow()
  74.     {
  75.     }
  76.  
  77.  
  78. void
  79. TWindow::CreateWindow(WindowType typeOfWindowToCreate /* = kNormalWindow */)
  80.     {
  81.     WindowPtr    behindWindow,oldFrontMostWindow;
  82.     
  83.     if (typeOfWindowToCreate == kModalWindow)
  84.         {
  85.         DebugStr((StringPtr) "\pModal windows aren’t supported yet");
  86.         fWindowType = kFloatingWindow;
  87.         return;
  88.         }
  89.     else if (typeOfWindowToCreate == kFloatingWindow)
  90.         {
  91.         behindWindow = (WindowPtr) -1;
  92.         oldFrontMostWindow = FrontWindow();
  93.  
  94.         fWindowType = kFloatingWindow;
  95.         }
  96.     else if (typeOfWindowToCreate == kNormalWindow)
  97.         {
  98.         behindWindow = LastFloatingWindow();
  99.  
  100.         fWindowType = kNormalWindow;
  101.         
  102.         if (behindWindow == kNoFloatingWindows)
  103.             oldFrontMostWindow = nil;
  104.         else
  105.             oldFrontMostWindow = (WindowPtr) ((WindowPeek) behindWindow)->nextWindow;
  106.         }
  107.  
  108.     fWindow = this->MakeNewWindow(behindWindow);
  109.     fIsVisible = ((WindowPeek) fWindow)->visible;
  110.  
  111.     if (fWindow)
  112.         {
  113.         SetWRefCon(fWindow,(long) this);
  114.  
  115.         if (typeOfWindowToCreate == kModalWindow)
  116.             {
  117.             DebugStr((StringPtr) "\pCan’t create Modal windows yet");
  118.             }
  119.         else if (typeOfWindowToCreate == kFloatingWindow)
  120.             {
  121.             ((WindowPeek) fWindow)->windowKind = kFloatingWindowKind;
  122.             
  123.             //    make sure the other window stays hilited
  124.             if (oldFrontMostWindow)
  125.                 HiliteAndActivateWindow(oldFrontMostWindow,true);
  126.             }
  127.         else if (typeOfWindowToCreate == kNormalWindow)
  128.             {
  129.             ((WindowPeek) fWindow)->windowKind = kNormalWindowKind;
  130.  
  131.             //    unhighlight the old front window
  132.             if (oldFrontMostWindow)
  133.                 HiliteAndActivateWindow(oldFrontMostWindow,false);
  134.  
  135.             //    hilite the new window…
  136.             HiliteAndActivateWindow(fWindow,true);
  137.             }
  138.         }
  139.     }
  140.  
  141.  
  142. void
  143. TWindow::AdjustCursor(EventRecord * /* anEvent */)
  144.     {
  145.     }
  146.  
  147. void
  148. TWindow::Idle(EventRecord * /* anEvent */)
  149.     {
  150.     }
  151.     
  152. void
  153. TWindow::Activate(Boolean /* activating */)
  154.     {
  155.     }
  156.     
  157. void
  158. TWindow::Draw(void)
  159.     {
  160.     }
  161.     
  162. void
  163. TWindow::Click(EventRecord * /* anEvent */)
  164.     {
  165.     }
  166.     
  167. void
  168. TWindow::KeyDown(EventRecord * /* anEvent */)
  169.     {
  170.     }
  171.  
  172.  
  173. void
  174. TWindow::Select(void)
  175.     {
  176.     WindowPtr    currentFrontWindow;
  177.     
  178.     if (fWindowType == kFloatingWindow)
  179.         currentFrontWindow = FrontWindow();
  180.     else if (fWindowType == kNormalWindow)
  181.         currentFrontWindow = FrontNonFloatingWindow();
  182.     else
  183.         {
  184.         }
  185.  
  186.     if (currentFrontWindow != fWindow)
  187.         {
  188.         if (fWindowType == kFloatingWindow)
  189.             BringToFront(fWindow);
  190.         else
  191.             {
  192.             WindowPtr    lastFloater = LastFloatingWindow();
  193.  
  194.             //    If there are no floating windows,
  195.             //    just call SelectWindow like the good ol’ days
  196.  
  197.             if (lastFloater == kNoFloatingWindows)
  198.                 SelectWindow(fWindow);
  199.             else
  200.                 {
  201.                 // Deactivate the window currently in front.
  202.  
  203.                 HiliteAndActivateWindow(currentFrontWindow,false);
  204.     
  205.                 // Bring it behind the last floating window and activate it.
  206.                 // Note that Inside Mac 1 states that you need to call PaintOne() and CalcVis() on a
  207.                 // window if you are using SendBehind() to bring it closer to the front.  With System 7,
  208.                 // this is no longer necessary.
  209.  
  210.                 SendBehind(fWindow,lastFloater);
  211.                 HiliteAndActivateWindow(fWindow,true);
  212.                 }
  213.             }
  214.         }
  215.     }
  216.  
  217.  
  218. void
  219. TWindow::Drag(Point startPoint)
  220.     {
  221.     GrafPtr        savePort;
  222.     KeyMap        theKeyMap;
  223.     Boolean        commandKeyDown = false;
  224.     RgnHandle    draggingRegion;
  225.     long        dragResult;
  226.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  227.     
  228.     if (WaitMouseUp())        //    de-bounce?
  229.         {
  230.         // Set up the Window Manager port.
  231.     
  232.         GetPort(&savePort);
  233.         SetPort(gWindowManagerPort);
  234.         SetClip(GetGrayRgn());
  235.  
  236.         // Check to see if the command key is down.
  237.     
  238.         GetKeys(theKeyMap);
  239.         commandKeyDown = ((theKeyMap[1] & 0x8000) != 0);
  240.         
  241.         if (commandKeyDown)
  242.             {
  243.             //    We’re not going to change window ordering,
  244.             //    so make sure that we don’t drag in front of
  245.             //    other windows which may be in front of ours.
  246.  
  247. //    11/16/94    <Windows.h> on ETO defines this routine to take
  248. //                a WindowPtr instead of a WindowPeek. When building
  249. //                with new headers (like those included with ETO 16)
  250. //                make sure to define USEOLDUNIVERSALHEADERS to 0.
  251.  
  252. #if    USEOLDUNIVERSALHEADERS
  253.             ClipAbove(windowAsWindowPeek);
  254. #else
  255.             ClipAbove((WindowPtr) windowAsWindowPeek);
  256. #endif
  257.             }
  258.         else if (fWindowType != kFloatingWindow)
  259.             {
  260.             //    We’re dragging a normal window, so make sure
  261.             //    that we don’t drag in front of any floating
  262.             //    windows.
  263.  
  264. //    11/16/94    <Windows.h> on ETO defines this routine to take
  265. //                a WindowPtr instead of a WindowPeek. When building
  266. //                with new headers (like those included with ETO 16)
  267. //                make sure to define USEOLDUNIVERSALHEADERS to 0.
  268.  
  269. #if    USEOLDUNIVERSALHEADERS
  270.             ClipAbove((WindowPeek) FrontNonFloatingWindow());
  271. #else
  272.             ClipAbove(FrontNonFloatingWindow());
  273. #endif
  274.             }
  275.         
  276.         //    Drag an outline of the window around the desktop.
  277.         //    NOTE: DragGrayRgn destroys the region passed in, so make a copy
  278.  
  279.         draggingRegion = NewRgn();
  280.         CopyRgn(windowAsWindowPeek->strucRgn,draggingRegion);
  281.         dragResult = DragGrayRgn(draggingRegion, startPoint, &gDeskRectangle, &gDeskRectangle, noConstraint, nil);
  282.         DisposeRgn(draggingRegion);
  283.  
  284.     
  285.         SetPort(savePort);    //    Get back to old port
  286.  
  287.         if ((dragResult != 0) && (dragResult != 0x80008000))
  288.             {
  289.             this->Nudge((short) (dragResult & 0xFFFF),(short) (dragResult >> 16));
  290.             }
  291.         }
  292.  
  293.     if (!commandKeyDown)
  294.         Select();
  295.     }
  296.  
  297. void
  298. TWindow::Nudge(short horizontalDistance, short verticalDistance)
  299.     {
  300.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  301.     short        newHorizontalPosition,newVerticalPosition;
  302.     
  303.     newHorizontalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.left + horizontalDistance;
  304.     newVerticalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.top + verticalDistance;
  305.  
  306.     MoveWindow(fWindow,newHorizontalPosition,newVerticalPosition,false);
  307.     }
  308.  
  309. void
  310. TWindow::Grow(Point startPoint)
  311.     {
  312.     GrafPtr    oldPort;
  313.     long    newSize;
  314.     Rect    oldWindowRect,resizeLimits;
  315.     
  316.     GetPort(&oldPort);
  317.     
  318.     GetWindowSizeLimits(&resizeLimits);
  319.     newSize = GrowWindow(fWindow,startPoint,&resizeLimits);
  320.     if (newSize)
  321.         {
  322.         oldWindowRect = fWindow->portRect;
  323.         SizeWindow(fWindow,(short) newSize,(short) (newSize >> 16),true);
  324.         SetPort(fWindow);
  325.         this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  326.         }
  327.     
  328.     SetPort(oldPort);
  329.     }
  330.  
  331.  
  332. void
  333. TWindow::Zoom(short zoomState)
  334.     {
  335.     GrafPtr        oldPort;
  336.     FontInfo    systemFontInfo;
  337.     short        titleBarHeight;
  338.     Rect        bestScreenRect,perfectWindowRect,scratchRect;
  339.     short        amountOffscreen;
  340.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  341.     GDHandle    bestDevice;
  342.     
  343.     GetPort(&oldPort);
  344.  
  345.     //    Figure out the height of the title bar so we can properly position
  346.     //    a window. The algorithm is stolen from the System 7.x 'WDEF' (0)
  347.     //
  348.     //    This probably isn’t the best thing to do: A better way might be 
  349.     //    to diff the structure and content region rectangles?
  350.  
  351.     SetPort(gWindowManagerPort);
  352.     GetFontInfo(&systemFontInfo);
  353.     titleBarHeight = (short) (systemFontInfo.ascent + systemFontInfo.descent + 4);
  354.     if ((titleBarHeight % 2) == 1)
  355.         titleBarHeight--;
  356.     if (titleBarHeight < kMinimumTitleBarHeight)
  357.         titleBarHeight = kMinimumTitleBarHeight;
  358.  
  359.  
  360.     //    Only do the voodoo magic if we are really “zooming” the window.
  361.  
  362.     if (zoomState == inZoomOut)
  363.         {
  364.         FindScreenRectWithLargestPartOfWindow(fWindow,&bestScreenRect,&bestDevice);
  365.         bestScreenRect.top += titleBarHeight;
  366.  
  367.         this->GetPerfectWindowSize(&perfectWindowRect);
  368.         OffsetRect(&perfectWindowRect,-perfectWindowRect.left,-perfectWindowRect.top);
  369.  
  370.         //    Take the zero-pined perfect window size and move it to
  371.         //    the top left of the    window’s content region.
  372.  
  373.         OffsetRect(&perfectWindowRect,(**windowAsWindowPeek->contRgn).rgnBBox.left,
  374.                                       (**windowAsWindowPeek->contRgn).rgnBBox.top);
  375.  
  376.         
  377.         //    Does perfectWindowRect fit completely on the best screen?
  378.         
  379.         SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  380.         if (!EqualRect(&perfectWindowRect, &scratchRect))
  381.             {
  382.             //    SectRect sez perfectWindowRect doesn’t completely fit
  383.             //    on the screen, so bump the window so that more of it fits.
  384.  
  385.             //    Make sure that the left edge of perfectWindowRect is forced
  386.             //    onto the best screen.  This is in case we are bumping
  387.             //    the window to the right.
  388.  
  389.             amountOffscreen = bestScreenRect.left - perfectWindowRect.left;
  390.             if (amountOffscreen > 0)
  391.                 {
  392.                 perfectWindowRect.left += amountOffscreen;
  393.                 perfectWindowRect.right += amountOffscreen;
  394.                 }
  395.  
  396.             //    Make sure that the left edge of perfectWindowRect is forced
  397.             //    onto the best screen.  This is in case we are bumping
  398.             //    the window downward to a new screen.
  399.     
  400.             amountOffscreen = bestScreenRect.top - perfectWindowRect.top;
  401.             if (amountOffscreen > 0)
  402.                 {
  403.                 perfectWindowRect.top += amountOffscreen;
  404.                 perfectWindowRect.bottom += amountOffscreen;
  405.                 }
  406.  
  407.             //    If right edge of window falls off the screen,
  408.             //        Move window to the left until the right edge IS on the screen
  409.             //        OR the left edge is at bestScreenRect.left
  410.  
  411.             amountOffscreen = perfectWindowRect.right - bestScreenRect.right;
  412.             if (amountOffscreen > 0)
  413.                 {
  414.                 //    Are we going to push the left edge offscreen? If so, change the
  415.                 //    offset so we move the window all the way over to the left.
  416.                 
  417.                 if ((perfectWindowRect.left - amountOffscreen) < bestScreenRect.left)
  418.                     amountOffscreen = perfectWindowRect.left - bestScreenRect.left;
  419.  
  420.                 perfectWindowRect.left -= amountOffscreen;
  421.                 perfectWindowRect.right -= amountOffscreen;
  422.                 }
  423.  
  424.             //    If bottom edge of window falls off the screen,
  425.             //        Move window to up until the bottom edge IS on the screen
  426.             //        OR the top edge is at bestScreenRect.top
  427.  
  428.             amountOffscreen = perfectWindowRect.bottom - bestScreenRect.bottom;
  429.             if (amountOffscreen > 0)
  430.                 {
  431.                 //    Are we going to push the top edge offscreen? If so, change the
  432.                 //    offset so we move the window just to the top.
  433.                 
  434.                 if ((perfectWindowRect.top - amountOffscreen) < bestScreenRect.top)
  435.                     amountOffscreen = perfectWindowRect.top - bestScreenRect.top;
  436.  
  437.                 perfectWindowRect.top -= amountOffscreen;
  438.                 perfectWindowRect.bottom -= amountOffscreen;
  439.                 }
  440.  
  441.             SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  442.             if (!EqualRect(&perfectWindowRect, &scratchRect))
  443.                 {
  444.                 //    The edges of the window still fall offscreen,
  445.                 //    so make the window smaller until it fits.
  446.                 
  447.                 if (perfectWindowRect.bottom > bestScreenRect.bottom)
  448.                     perfectWindowRect.bottom = bestScreenRect.bottom;
  449.  
  450.                 //    If the right edge is still falling off,
  451.                 //        save space for Finder’s disk icons as well.
  452.  
  453.                 if (perfectWindowRect.right > bestScreenRect.right)
  454.                     {
  455.                     perfectWindowRect.right = bestScreenRect.right;
  456.                     
  457.                     //    If we were on the main screen, leave room for Finder icons, too.
  458.                     
  459.                     if (bestDevice == GetMainDevice())
  460.                         perfectWindowRect.right -= kSpaceForFinderIcons;
  461.                     }
  462.                 }
  463.             }
  464.  
  465.         //    Stash our new rectangle inside of the Window’s dataHandle
  466.         //    so that ZoomWindow does the right thing.
  467.         
  468.         (**((WStateDataHandle) (windowAsWindowPeek->dataHandle))).stdState = perfectWindowRect;
  469.         }
  470.  
  471.     //    HEY YOU! Don’t forget to set the port to the window being zoomed
  472.     //    Why, you ask? Because IM-IV-50 says to; otherwise you die
  473.     
  474.     SetPort(fWindow);
  475.  
  476.     Rect    oldWindowRect = fWindow->portRect;
  477.     
  478.     ZoomWindow(fWindow,zoomState,false);
  479.     this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  480.  
  481.     SetPort(oldPort);
  482.     }
  483.  
  484. void
  485. TWindow::ShowHide(Boolean showFlag)
  486.     {
  487.     //    Here we need the “::” in front of ShowHide to indicate we are calling
  488.     //    the global function, and not the method ShowHide. Unintended recursion
  489.     //    can do bad things to the unsuspecting programmer.
  490.     
  491.     //    Some C++ programmers would always prepend the “::” on function calls.
  492.     
  493.     ::ShowHide(fWindow,showFlag);
  494.     fIsVisible = showFlag;
  495.     }
  496.     
  497.  
  498. Boolean
  499. TWindow::EventFilter(EventRecord * /* theEvent */)
  500.     {
  501.     return false;
  502.     }
  503.     
  504.  
  505. void
  506. TWindow::GetPerfectWindowSize(Rect *perfectSize)
  507.     {
  508.     *perfectSize = qd.screenBits.bounds;
  509.     }
  510.  
  511. void
  512. TWindow::GetWindowSizeLimits(Rect *limits)
  513.     {
  514.     limits->top = limits->left = kMinimumWindowSize;
  515.     limits->right = gDeskRectangle.right - gDeskRectangle.left;
  516.     limits->bottom = gDeskRectangle.bottom - gDeskRectangle.top;
  517.     }
  518.  
  519. void
  520. TWindow::AdjustForNewWindowSize(Rect * /* oldRect */, Rect * /* newSize */)
  521.     {
  522.     }
  523.  
  524.  
  525. Boolean
  526. TWindow::IsVisible(void)
  527.     {
  528.     return fIsVisible;
  529.     }
  530.  
  531.  
  532. Boolean
  533. TWindow::CanClose(void)
  534.     {
  535.     return true;
  536.     }
  537.  
  538.  
  539. Boolean
  540. TWindow::Close(void)
  541.     {
  542.     WindowPtr    newFrontWindow = nil;
  543.     
  544.     if (FrontNonFloatingWindow() == fWindow)
  545.         newFrontWindow = (WindowPtr) ((WindowPeek) fWindow)->nextWindow;
  546.  
  547.     this->Activate(false);
  548.     DisposeWindow(fWindow);
  549.  
  550.     if (newFrontWindow)
  551.         HiliteAndActivateWindow(newFrontWindow,true);
  552.  
  553.     return true;
  554.     }
  555.  
  556.  
  557. Boolean
  558. TWindow::DeleteAfterClose(void)
  559.     {
  560.     return true;
  561.     }
  562.  
  563.  
  564. void
  565. TWindow::AdjustMenusBeforeMenuSelection(void)
  566.     {
  567.     }
  568.  
  569.     
  570. void
  571. TWindow::DoMenuSelection(short /* menu */, short /* item */)
  572.     {
  573.     }
  574.     
  575.  
  576. void
  577. TWindow::DoMenuCommand(unsigned long /* menuCommand */)
  578.     {
  579.     }
  580.  
  581.  
  582. OSErr
  583. TWindow::HandleDrag(DragTrackingMessage dragMessage,DragReference theDrag)
  584.     {
  585.     OSErr    result = dragNotAcceptedErr;
  586.     
  587.     switch (dragMessage)
  588.         {
  589.         case    dragTrackingEnterWindow:
  590.             result = this->DragEnterWindow(theDrag);
  591.             break;
  592.         
  593.         case    dragTrackingInWindow:
  594.             result = this->DragInWindow(theDrag);
  595.             break;
  596.             
  597.         case    dragTrackingLeaveWindow:
  598.             result = this->DragLeaveWindow(theDrag);
  599.             break;
  600.             
  601.         default:
  602.             break;
  603.         }
  604.  
  605.     return result;
  606.     }
  607.  
  608.  
  609. OSErr
  610. TWindow::DragEnterWindow(DragReference /* theDrag */)
  611.     {
  612.     return dragNotAcceptedErr;
  613.     }
  614.  
  615.  
  616. OSErr
  617. TWindow::DragInWindow(DragReference /* theDrag */)
  618.     {
  619.     return dragNotAcceptedErr;
  620.     }
  621.  
  622.  
  623. OSErr
  624. TWindow::DragLeaveWindow(DragReference /* theDrag */)
  625.     {
  626.     return dragNotAcceptedErr;
  627.     }
  628.     
  629.  
  630. OSErr
  631. TWindow::HandleDrop(DragReference /* theDrag */)
  632.     {
  633.     return dragNotAcceptedErr;
  634.     }
  635.  
  636.  
  637. ///////////////////////////////////////////////////////////////////////////
  638. //
  639. //    Utility Functions used for floating windows
  640. //
  641.  
  642. TWindow *
  643. GetWindowObject(WindowPtr aWindow)
  644.     {
  645.     short    wKind;
  646.     
  647.     if (aWindow != nil)
  648.         {
  649.         wKind = ((WindowPeek) aWindow)->windowKind;
  650.  
  651.         if (wKind >= userKind)
  652.             {
  653.             //    All windowKinds >= userKind are based upon TWindow
  654.  
  655.             return (TWindow *) GetWRefCon(aWindow);
  656.             }
  657.         }
  658.     return (TWindow *) nil;
  659.     }
  660.  
  661.  
  662. ////////////////////////////////////////////////////////////////////////
  663. //
  664. //    Utility functions
  665.  
  666.  
  667. pascal WindowPtr
  668. GetNewColorOrBlackAndWhiteWindow(short windowID, void *wStorage, WindowPtr behind)
  669.     {
  670.     if (gHasColorQuickdraw)
  671.         return GetNewCWindow(windowID,wStorage,behind);
  672.     else
  673.         return GetNewWindow(windowID,wStorage,behind);
  674.     }
  675.  
  676.  
  677. pascal WindowPtr
  678. NewColorOrBlackAndWhiteWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title, Boolean visible, short theProc, WindowPtr behind, Boolean goAwayFlag, long refCon)
  679.     {
  680.     if (gHasColorQuickdraw)
  681.         return NewCWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  682.     else
  683.         return NewWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  684.     }
  685.  
  686.  
  687. void
  688. DrawJustTheGrowIcon(WindowPtr aWindow)
  689.     {
  690.     GrafPtr        savedPort;
  691.     RgnHandle    savedClip = NewRgn();
  692.     Rect        growBoxRect;
  693.     
  694.     GetPort(&savedPort);
  695.     SetPort(aWindow);
  696.     GetClip(savedClip);
  697.  
  698.     //    clip to just the bottom right corner of the window
  699.     
  700.     growBoxRect.top = aWindow->portRect.bottom - kScrollbarWidth;
  701.     growBoxRect.bottom = aWindow->portRect.bottom;
  702.     growBoxRect.left = aWindow->portRect.right - kScrollbarWidth;
  703.     growBoxRect.right = aWindow->portRect.right;
  704.     ClipRect(&growBoxRect);
  705.  
  706.     DrawGrowIcon(aWindow);
  707.  
  708.     SetClip(savedClip);
  709.     DisposeRgn(savedClip);    
  710.  
  711.     SetPort(savedPort);
  712.     }
  713.     
  714.  
  715. WindowPtr
  716. LastFloatingWindow(void)
  717.     {
  718.     WindowPeek    aWindow = (WindowPeek) FrontWindow();
  719.     WindowPtr    lastFloater = (WindowPtr) kNoFloatingWindows;
  720.     
  721.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  722.         {
  723.         if (aWindow->visible)
  724.             lastFloater = (WindowPtr) aWindow;
  725.  
  726.         aWindow = (WindowPeek) aWindow->nextWindow;
  727.         }
  728.     return(lastFloater);
  729.     }
  730.  
  731.  
  732. WindowPtr
  733. FrontNonFloatingWindow(void)
  734.     {
  735.     WindowPeek    aWindow = (WindowPeek) LMGetWindowList();
  736.  
  737.     //    Skip over floating windows
  738.         
  739.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  740.         aWindow = (WindowPeek) aWindow->nextWindow;
  741.  
  742.     //    Skip over invisible, but otherwise normal windows
  743.     
  744.     while (aWindow && (aWindow->visible == 0))
  745.         aWindow = (WindowPeek) aWindow->nextWindow;
  746.         
  747.     return (WindowPtr) aWindow;
  748.     }
  749.  
  750.  
  751. void
  752. HiliteAndActivateWindow(WindowPtr aWindow,Boolean active)
  753.     {
  754.     GrafPtr        oldPort;
  755.     TWindow    *    wobj = GetWindowObject(aWindow);
  756.     
  757.     if (aWindow)
  758.         {
  759.         HiliteWindow(aWindow,active);
  760.  
  761.         if (wobj != nil)
  762.             {
  763.             GetPort(&oldPort);
  764.             SetPort(aWindow);
  765.             wobj->Activate(active);
  766.             SetPort(oldPort);
  767.             }    
  768.         }
  769.     }
  770.  
  771. void
  772. SuspendResumeWindows(Boolean resuming)
  773.     {
  774.     //    When we suspend/resume, hide/show all the visible floaters
  775.     
  776.     HiliteShowHideFloatingWindows(resuming,true);
  777.     }
  778.  
  779. void
  780. HiliteWindowsForModalDialog(Boolean hiliting)
  781.     {
  782.     //    When we display a modal dialog, we need to unhighlight
  783.     //    all visible floaters. We also need to re-hilite them
  784.     //    afterwards.
  785.     
  786.     HiliteShowHideFloatingWindows(hiliting,false);
  787.     }
  788.  
  789. void
  790. HiliteShowHideFloatingWindows(Boolean hiliting,Boolean dohiding)
  791.     {
  792.     WindowPeek    aWindow;
  793.     TWindow *    wobj;
  794.     
  795.     HiliteAndActivateWindow(FrontNonFloatingWindow(),hiliting);
  796.  
  797.     aWindow = (WindowPeek) LMGetWindowList();
  798.     while (aWindow && aWindow->windowKind == kFloatingWindowKind)
  799.         {
  800.         wobj = GetWindowObject((WindowPtr) aWindow);
  801.         
  802.         //    If we are hiding or showing, only hide/show windows
  803.         //    that were visible to begin with.
  804.         
  805.         //    NOTE:    We use our copy of the visible flag so we can
  806.         //            automatically show floaters on a resume event.
  807.         
  808.         //    NOTE:    Since this isn’t a method of TWindow, we don’t
  809.         //            really need the “::” on ShowHide, but as long
  810.         //            as we’re trying to avoid ambiguity.
  811.         
  812.         if (dohiding && (wobj != nil) && (wobj->IsVisible()))
  813.             ::ShowHide((WindowPtr) aWindow,hiliting);
  814.             
  815.         //    All floaters are hilited if any floater is hilited
  816.  
  817.         HiliteWindow((WindowPtr) aWindow,hiliting);
  818.         aWindow = (WindowPeek) aWindow->nextWindow;
  819.         }
  820.     }
  821.  
  822.  
  823. ///////////////////////////////////////////////////////////////////////////
  824. //
  825. //    Routines used for dealing with windows and multiple screens
  826. //
  827.  
  828. pascal void
  829. CalculateWindowAreaOnDevice(short /* depth */,short /* deviceFlags */,GDHandle targetDevice,long userData)
  830.     {
  831.     CalcWindowAreaDeviceLoopUserData *    deviceLoopDataPtr;
  832.     long                                windowAreaOnThisScreen;
  833.     Rect                                windowRectOnThisScreen;
  834.     
  835.     deviceLoopDataPtr = (CalcWindowAreaDeviceLoopUserData *) userData;
  836.  
  837.     SectRect(&deviceLoopDataPtr->fWindowBounds, &(**targetDevice).gdRect,&windowRectOnThisScreen);
  838.     OffsetRect(&windowRectOnThisScreen,-windowRectOnThisScreen.left,-windowRectOnThisScreen.top);
  839.     windowAreaOnThisScreen = windowRectOnThisScreen.right * windowRectOnThisScreen.bottom;
  840.  
  841.     if (windowAreaOnThisScreen > deviceLoopDataPtr->fLargestArea)
  842.         {
  843.         deviceLoopDataPtr->fLargestArea = windowAreaOnThisScreen;
  844.         deviceLoopDataPtr->fScreenWithLargestPartOfWindow = targetDevice;
  845.         }
  846.     }
  847.  
  848.  
  849. DeviceLoopDrawingUPP CallCalcWindowAreaOnDevice = NewDeviceLoopDrawingProc(&CalculateWindowAreaOnDevice);
  850.  
  851.  
  852. void
  853. FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect,GDHandle * theBestDevice)
  854.     {
  855.     RgnHandle                            copyOfWindowStrucRgn;
  856.     CalcWindowAreaDeviceLoopUserData    deviceLoopData;
  857.  
  858.     //    Use DeviceLoop to find out what GDevice contains the largest
  859.     //    portion of the supplied window.
  860.     //
  861.     //    NOTE:    Assumes thePort == the Window Manager Port because we using
  862.     //            the window strucRgn, not contRgn.
  863.  
  864.     deviceLoopData.fScreenWithLargestPartOfWindow = nil;
  865.     deviceLoopData.fLargestArea = -1;
  866.     deviceLoopData.fWindowBounds = (**(((WindowPeek) aWindow)->contRgn)).rgnBBox;
  867.     
  868.     copyOfWindowStrucRgn = NewRgn();
  869.     CopyRgn(((WindowPeek) aWindow)->strucRgn,copyOfWindowStrucRgn);
  870.  
  871.     DeviceLoop(copyOfWindowStrucRgn,CallCalcWindowAreaOnDevice,(long) &deviceLoopData,singleDevices);    
  872.  
  873.     DisposeRgn(copyOfWindowStrucRgn);
  874.     
  875.     *theBestDevice = deviceLoopData.fScreenWithLargestPartOfWindow;
  876.     *theBestScreenRect = (**(deviceLoopData.fScreenWithLargestPartOfWindow)).gdRect;
  877.  
  878.     //    Leave some space around the edges of the screen so window look good, AND
  879.     //    if the best device is the main screen, leave space for the Menubar
  880.     
  881.     InsetRect(theBestScreenRect,kScreenEdgeSlop,kScreenEdgeSlop);
  882.     if (GetMainDevice() == deviceLoopData.fScreenWithLargestPartOfWindow)
  883.         theBestScreenRect->top += GetMBarHeight();
  884.     }
  885.  
  886.  
  887. ///////////////////////////////////////////////////////////////////////////
  888. //
  889. //    Drag Manager callback routines which dispatch to a window’s method
  890. //
  891.  
  892. pascal OSErr
  893. CallWindowDragTrackingHandler(DragTrackingMessage dragMessage,WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  894.     {
  895.     TWindow *wobj = GetWindowObject(theWindow);
  896.     
  897.     if (wobj)
  898.         return(wobj->HandleDrag(dragMessage,theDrag));
  899.     else
  900.         return dragNotAcceptedErr;
  901.     }
  902.  
  903.     
  904. pascal OSErr
  905. CallWindowDragReceiveHandler(WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  906.     {
  907.     TWindow *wobj = GetWindowObject(theWindow);
  908.     
  909.     if (wobj)
  910.         return(wobj->HandleDrop(theDrag));
  911.     else
  912.         return dragNotAcceptedErr;
  913.     }
  914.